<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:m="livcos.org/ns/xslt/function/math"
	exclude-result-prefixes="f xs">
	
	<xsl:import href="../../common/base.xsl"/>
	
	
	<xsl:function name="f:castV" as="xs:double*">
		<xsl:param name="val"/>
		<xsl:for-each select="tokenize($val, '[, ]+')">
			<xsl:value-of select="."/>
		</xsl:for-each>
	</xsl:function>
	
	<xsl:function name="f:rotateV" as="xs:double*">
		<xsl:param name="v" as="xs:double*"/>
		<xsl:param name="degree" as="xs:double"/>
		<xsl:variable name="r" select="m:rad($degree)"/>
		<xsl:value-of select="$v[1] * m:cos($r) - $v[2] * m:sin($r)"/>
		<xsl:value-of select="$v[1] * m:sin($r) + $v[2] * m:cos($r)"/>
	</xsl:function>
	
	<xsl:function name="f:addV" as="xs:double*">
		<xsl:param name="v1" as="xs:double*"/>
		<xsl:param name="v2" as="xs:double*"/>
		<xsl:for-each select="$v1">
			<xsl:variable name="i" select="position()"/>
			<xsl:value-of select=". + $v2[$i]"/>
		</xsl:for-each>
	</xsl:function>
	
	<xsl:function name="f:subV" as="xs:double*">
		<xsl:param name="v1" as="xs:double*"/>
		<xsl:param name="v2" as="xs:double*"/>
		<xsl:for-each select="$v1">
			<xsl:variable name="i" select="position()"/>
			<xsl:value-of select=". - $v2[$i]"/>
		</xsl:for-each>
	</xsl:function>
	
	<xsl:function name="f:mulV" as="xs:double*">
		<xsl:param name="v" as="xs:double*"/>
		<xsl:param name="s" as="xs:double"/>
		<xsl:for-each select="$v">
			<xsl:value-of select=". * $s"/>
		</xsl:for-each>
	</xsl:function>
	
	<xsl:function name="f:lenV" as="xs:double">
		<xsl:param name="v" as="xs:double*"/>
		<xsl:value-of select="m:sqrt($v[1] * $v[1] + $v[2] * $v[2])"/>
	</xsl:function>
	
	<xsl:function name="f:printV" as="xs:string">
		<xsl:param name="v" as="xs:double*"/>
		<xsl:variable name="_retVal">
			<xsl:for-each select="$v">
				<xsl:if test="position() > 1">,</xsl:if>
				<xsl:value-of select="round(.)"/>
			</xsl:for-each>
		</xsl:variable>
		<xsl:value-of select="$_retVal"/>
	</xsl:function>
	
</xsl:stylesheet>
